package dji.ux.ext.panel.camera;

import android.util.Log;

import androidx.annotation.NonNull;

import dji.common.camera.CameraVideoStreamSource;
import dji.common.camera.SettingsDefinitions;
import dji.common.camera.ThermalMeasurementMode;
import dji.common.error.DJIError;
import dji.common.util.CommonCallbacks;
import dji.sdk.camera.Camera;
import dji.sdk.camera.Lens;
import dji.sdk.sdkmanager.DJISDKManager;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.WidgetModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;


/**
 * Widget Model for the {@link CameraH20TModel} used to define
 * the underlying logic and communication
 */
class CameraH20TModel extends WidgetModel {

    private Camera h20TCamera;


    CameraH20TModel(@NonNull DJISDKModel djiSdkModel,
                           @NonNull ObservableInMemoryKeyedStore keyedStore) {
        super(djiSdkModel, keyedStore);
    }

    boolean isH20TCamera(){
        if (DJISDKManager.getInstance().getProduct() == null ||
                DJISDKManager.getInstance().getProduct().getCameras() == null)
            return false;

        boolean isH20TCamera = false;
        for (Camera camera : DJISDKManager.getInstance().getProduct().getCameras()){
            if (camera.getDisplayName().equals(Camera.DisplayNameZenmuseH20T) ||
                    camera.getDisplayName().equals(Camera.DisplayNameZenmuseH20)){
                isH20TCamera = true;
                h20TCamera = camera;
                break;
            }
        }

        return isH20TCamera;
    }


    void getH20TCameraSource(CommonCallbacks.CompletionCallbackWith<CameraVideoStreamSource> callback) {
        if (h20TCamera == null)
            return;

        h20TCamera.getCameraVideoStreamSource(callback);
    }

    void setH20TCameraSource(CameraVideoStreamSource streamSource, CommonCallbacks.CompletionCallback callback) {
        if (h20TCamera == null)
            return;

        h20TCamera.setCameraVideoStreamSource(streamSource,callback);
    }

    void getDisplayMode(CommonCallbacks.CompletionCallbackWith<SettingsDefinitions.DisplayMode> callback) {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.getDisplayMode(callback);
            }
        }
    }

    void setDisplayMode(SettingsDefinitions.DisplayMode displayMode, CommonCallbacks.CompletionCallback callback) {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.setDisplayMode(displayMode, callback);
            }
        }
    }

    void setPIPPosition() {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.setPIPPosition(SettingsDefinitions.PIPPosition.SIDE_BY_SIDE, new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onResult(DJIError djiError) {
                        if (djiError != null)
                        Log.e("UXESDK", djiError.getDescription());
                    }
                });
            }
        }
    }

    void getThermalDigitalZoom(CommonCallbacks.CompletionCallbackWith<SettingsDefinitions.ThermalDigitalZoomFactor> callback) {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.getThermalDigitalZoomFactor(callback);
            }
        }
    }

    void setThermalDigitalZoom(SettingsDefinitions.ThermalDigitalZoomFactor zoomFactor, CommonCallbacks.CompletionCallback callback) {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.setThermalDigitalZoomFactor(zoomFactor, callback);
            }
        }
    }

    void setThermalMeasurementMode(ThermalMeasurementMode mode, CommonCallbacks.CompletionCallback callback) {

        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.setThermalMeasurementMode(mode, callback);
            }
        }
    }

    void getThermalMeasurementMode(CommonCallbacks.CompletionCallbackWith<ThermalMeasurementMode> callback) {
        if (h20TCamera == null || h20TCamera.getLenses() == null)
            return;

        for (Lens lens : h20TCamera.getLenses()){
            if (lens.getType() == SettingsDefinitions.LensType.INFRARED_THERMAL){
                lens.getThermalMeasurementMode(callback);
            }
        }
    }



    @Override
    protected void inSetup() {

    }

    @Override
    protected void inCleanup() {
        //Nothing to cleanup
    }

    @Override
    protected void updateStates() {
        //Nothing to update
    }


}
